package cn.edu.scnu.service;

import cn.edu.scnu.utils.UploadUtil;
import cn.edu.scnu.vo.PicUploadResult;
import cn.edu.scnu.vo.ResultCode;
import cn.edu.scnu.vo.SysResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class PicService {
    @Value("${pic.pathDirPrefix}")
    private String pathDirPrefix;
    @Value("${pic.urlPrefix}")
    private String urlPreparefix;

    public PicUploadResult picUpload(HttpServletRequest request, MultipartFile pic) {
//        pathDirPrefix=this.getClass().getResource("/").getPath().replace("supermatch-microservice-pic/target/classes/"
//                ,"/nginx-1.9.9/supermatch_image/");
        PicUploadResult result = new PicUploadResult();
//        判断后缀是否合法
        String originName = pic.getOriginalFilename();
        String typeName  = originName.substring(originName.lastIndexOf("."));
        boolean isLegal = typeName.matches(".(jpg|gif|jpeg|gif|bmp|png|JPG|GIF|PNG|GIF|BMP|JPEG)$");
        if (!isLegal){
            result.setError(1);
            return result;
        }
//        木马判断
        try {
            BufferedImage bufImg = ImageIO.read(pic.getInputStream());
            bufImg.getHeight();
            bufImg.getWidth();
        }catch (IOException e){
            e.printStackTrace();
            result.setError(1);
            return result;
        }
//      创建上传路径
        String dir  = UploadUtil.getUploadPath(originName,"upload");
//        创建访问的静态路径pathDir
        String pathDir = pathDirPrefix+dir;
        File file = new File(pathDir);
        if (!file.exists()){
            file.mkdirs();
        }
//        创建urlPrefix
        String urlPrefix = urlPreparefix + dir;
//        拼接图片名称 j将图片重命名uuid表示图片访问名称
        String fileName = UUID.randomUUID().toString()+typeName;
        //上传文件到磁盘路径
        try{
            pic.transferTo(new File(pathDir+fileName));
        } catch (IOException e) {
            e.printStackTrace();
            result.setError(1);
            return result;
        }
        //返回urlPrefix+图片名称的路径
        result.setUrl(urlPrefix + fileName);
        return result;
    }

    public SysResult deletePic(String picpath) {
        picpath = picpath.replace("www.supermatch.com/", "D:/gitspace/supermatch/nginx-1.9.9/");
        File file = new File(picpath);
        if (!file.exists()) {
            return SysResult.build(ResultCode.NOT_FOUND, "本地图片不存在", null);
        } else {
            try {
                file.delete();
                return SysResult.build(ResultCode.NO_CONTENT, "图片删除成功", null);
            } catch (Exception e) {
                e.printStackTrace();
                return SysResult.build(ResultCode.FAIL, e.getMessage(), null);
            }
        }
    }
}
